﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YRevitJig;
using Transfer.Utils;

namespace YArchitech.Plumbing
{
	public class DrawMultiPipeFromExistJig : DrawMultiPipeJig
	{
		public XYZ StartFromPoint { get; set; }

		public bool IsFirstExecution { get; set; }

		public bool IsAddFromPoint { get; set; }

		public PipeFromInfo InfoOfPipeFrom { get; set; }

		public double? SettingDistance { get; set; }

		public DrawMultiPipeFromExistJig(UIDocument uiDoc) : base(uiDoc)
		{
			base.PipeIntervals = new List<double>();
			this.IsFirstExecution = true;
			this.IsAddFromPoint = true;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null)
			{
				try
				{
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					XYZ xyz2 = null;
					if (this.InfoOfPipeFrom != null && this.InfoOfPipeFrom.PipesFrom.Count > 0 && this.IsFirstExecution)
					{
						this.InfoOfPipeFrom.SortedPipes = base.sortPipes(this.InfoOfPipeFrom.PipesFrom, this.InfoOfPipeFrom.PointFrom);
						if (this.InfoOfPipeFrom.SortedPipes != null && this.InfoOfPipeFrom.SortedPipes.Count > 0)
						{
							Line line = (this.InfoOfPipeFrom.SortedPipes[0].Location as LocationCurve).Curve as Line;
							XYZ hyendPoint = line.GetEndPoint(0);
							XYZ hyendPoint2 = line.GetEndPoint(1);
							XYZ xyz3 = hyendPoint2 - hyendPoint;
							if (Geometry.GreaterThan(xyz3.Y, 0.0))
							{
								xyz3 = hyendPoint - hyendPoint2;
							}
							if (Geometry.IsEqual(xyz3.Y, 0.0) && Geometry.LessThan(xyz3.X, 0.0))
							{
								xyz3 = hyendPoint - hyendPoint2;
							}
							IntersectionResult intersectionResult = line.Project(this.InfoOfPipeFrom.PointFrom);
							XYZ xyz4 = intersectionResult.XYZPoint - this.InfoOfPipeFrom.PointFrom;
							this.StartFromPoint = this.InfoOfPipeFrom.PointFrom.Add(xyz4).Add(xyz3.Normalize() * intersectionResult.Distance);
							xyz2 = new XYZ(xyz.X, xyz.Y, this.StartFromPoint.Z);
							List<DrawPipeStruct> pipeInfo = DrawMultiPipeUtility.GetPipeInfo(base.UiDoc.Document, this.InfoOfPipeFrom.SortedPipes, this.SettingDistance);
							base.PipeIntervals.Clear();
							foreach (DrawPipeStruct drawPipeStruct in pipeInfo)
							{
								base.PipeIntervals.Add(drawPipeStruct.offSet);
							}
						}
					}
					bool flag = false;
					if (this.PointsList.Count > 0 || this.IsFirstExecution)
					{
						if (this.PointsList.Count > 0)
						{
							xyz2 = new XYZ(xyz.X, xyz.Y, this.PointsList[0].Z);
						}
						else
						{
							xyz2 = new XYZ(xyz.X, xyz.Y, this.StartFromPoint.Z);
						}
						XYZ axisPoint = DrawMultiPipeUtility.GetAxisPoint(base.UIDoc, xyz2, this.PointsList[this.PointsList.Count - 1]);
						if (axisPoint != null)
						{
							xyz2 = axisPoint;
						}
						this.PointsList.Add(xyz2);
						flag = true;
						this.IsFirstExecution = false;
					}
					this.PipeLines = DrawMultiPipeUtility.DrawMultiLines(this.PointsList, this.StartFromPoint, xyz2, base.PipeIntervals, this.IsAddFromPoint);
					foreach (List<LineInfo> list in this.PipeLines)
					{
						foreach (LineInfo lineInfo in list)
						{
							new XLine2D(base.Revit2Client(lineInfo.StartPoint), base.Revit2Client(lineInfo.EndPoint)).Draw(g);
						}
					}
					if (flag)
					{
						this.PointsList.Remove(xyz2);
					}
				}
				catch (Exception ex)
				{
					ex.ToString();
					throw ex;
				}
			}
		}
	}
}
